<%@ page language="java" contentType="text/html; charset=windows-1255"
	pageEncoding="windows-1255"%>
<%@page import="logic.newLogic2.Facade3"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="com.sun.org.apache.xalan.internal.xsltc.compiler.sym"%>
<html>
<head>
<meta http-equiv="Content-Type"
	content="text/html; charset=windows-1255">
<title>Insert title here</title>
</head>
<body>


<%
	Facade3 f = Facade3.getInstance();
	String form = request.getParameter("form");
	if (form.equals("regUser")) {
		String name = request.getParameter("userName");
		String pass = request.getParameter("password");
		if (f.login(name, pass)) {
			session.setAttribute("currentUser", name);
			session.setAttribute("type", (f.isManager(name) ? "manager" : (f.isAdmin(name) ? "admin"
					: "user")));
			response.sendRedirect("userPanel.jsp");
		} else {
			response
					.sendRedirect("login.jsp?Error=\"Wrong username or password\"");
		}
	} else if (form.equals("newUser")) {
		String button = request.getParameter("action");
		if("Cancel".equals(button)){
			response.sendRedirect("login.jsp");
			return;
		}

		String name = request.getParameter("name");
		String pass1 = request.getParameter("password");
		String pass2 = request.getParameter("rePassword");
		String day = request.getParameter("day");
		String month = request.getParameter("month");
		String year = request.getParameter("year");
		String sex = request.getParameter("sex");

		switch (f.registerUser(name, pass1, pass2, day, month, year,
				sex)) {
		case 0:
			session.setAttribute("currentUser", name);
			session.setAttribute("type", "user");
			response.sendRedirect("userPanel.jsp");
			break;
		case 1:
			response
					.sendRedirect("userRegistration.jsp?Error=passwords dont match");
			break;
		case 2:
			response
					.sendRedirect("userRegistration.jsp?Error=missing mandatory field");
			break;
		case 3:
			response
					.sendRedirect("userRegistration.jsp?Error=user name is taken");
			break;
		case 4:
			response
					.sendRedirect("userRegistration.jsp?Error=SQL error");
			break;
		default:
			response
					.sendRedirect("userRegistration.jsp?Error=unknown error");
			break;
		}
	} else if (form.equals("userPanel")) {
		String button = request.getParameter("action");
		if ("Change My Personal Details".equals(button)) {
			response.sendRedirect("changeDetails.jsp");
		} else if ("I Want To Rate A Movie".equals(button)) {
			response.sendRedirect("movieListForRating.jsp?start=0");
		} else if ("What movies should I watch?".equals(button)) {
			response.sendRedirect("recommendationList.jsp?start=0");
		} else if ("Delete a user".equals(button)) {
			response.sendRedirect("removeUser.jsp");
		} else if ("Delete a movie".equals(button)) {
			response.sendRedirect("removeMovie.jsp");
		} else if ("Add a movie".equals(button)) {
			response.sendRedirect("addMovie.jsp");
		} else if ("Administrator permission".equals(button)) {
			response.sendRedirect("setAdmin.jsp");
		} else if ("Manager permission".equals(button)) {
			response.sendRedirect("setManager.jsp");
		} else if ("Reports".equals(button)) {
			response.sendRedirect("reportPanel.jsp");
		}
	} else if (form.equals("changeDetails")){
		String name = (String) session.getAttribute("currentUser");
		String oldpass = request.getParameter("oldPassword");
		String pass1 = request.getParameter("newPassword");
		String pass2 = request.getParameter("reNewPassword");
		String day = request.getParameter("day");
		String month = request.getParameter("month");
		String year = request.getParameter("year");
		String sex = request.getParameter("sex");
		switch (f.changeDetails(name, oldpass, pass1, pass2, day, month, year, sex)){
		case 0:
			response.sendRedirect("userPanel.jsp");
			break;
		case 1:
			response.sendRedirect("changeDetails.jsp?Error=old password does not match");
			break;
		case 2:
			response.sendRedirect("changeDetails.jsp?Error=new password error");
			break;
		default:
			response.sendRedirect("changeDetails.jsp?Error=unknown error");
			break;
		}

	} else if (form.equals("rateMovie")) {
			String movie = request.getParameter("movie");
			response.sendRedirect("addRating.jsp?movie="+movie);
	} else if (form.equals("addRating")) {
		String movie = request.getParameter("movie");
		String rating = request.getParameter("Rating");
		String description = request.getParameter("Description");
		String user = (String) session.getAttribute("currentUser");

		f.addRating( user , movie, rating , description);
		response.sendRedirect("userPanel.jsp");
	} else if (form.equals("addMovie")) {
		String name = request.getParameter("name");
		if ("".equals(name)){
			response.sendRedirect("addMovie.jsp?Error=Movie Name field cannot be empty");
			return;
			}
		String cat = request.getParameter("Category");
		if ("".equals(cat)){
			response.sendRedirect("addMovie.jsp?Error=Category field cannot be empty");
			return;
			}
		String dirc = request.getParameter("Director");
		if ("".equals(dirc)){
			response.sendRedirect("addMovie.jsp?Error=Director field cannot be empty");
			return;
			}
		String lang = request.getParameter("Language");
		if ("".equals(lang)){
			response.sendRedirect("addMovie.jsp?Error=Language field cannot be empty");
			return;
			}
		String len = request.getParameter("Length");
		if ("".equals(len)){
			response.sendRedirect("addMovie.jsp?Error=Length field cannot be empty");
			return;
			}
		String year = request.getParameter("Year");
		if ("".equals(year)){
			response.sendRedirect("addMovie.jsp?Error=Year field cannot be empty");
			return;
			}
		String pic = request.getParameter("Picture");

		if (f.addMovie(name, cat, dirc, lang, "0", "0", year, len, pic)) {
			response.sendRedirect("addMovie.jsp?Error=Movie succesfully added");
		} else {
			response.sendRedirect("addMovie.jsp?Error=Movie already exists");
		}
	} else if (form.equals("removeUser")) {
		String button = request.getParameter("action");
		if ("Menu".equals(button)) {
			response.sendRedirect("userPanel.jsp");
		}else{
			if (Facade3.getInstance().deleteUser(request.getParameter("user"),(String)session.getAttribute("currentUser"))){
				response.sendRedirect("removeUser.jsp?Error=User Succesfuly removed");
			} else {
				response.sendRedirect("removeUser.jsp?Error=User does not exist");
			}
		}

	} else if (form.equals("setAdmin")) {
		String button = request.getParameter("action");
		if ("Menu".equals(button)) {
			response.sendRedirect("userPanel.jsp");
		}else{
			Facade3.getInstance().setAdmin(request.getParameter("user"));
			response.sendRedirect("setAdmin.jsp?msg=Permission added successfully");
		}
	} else if (form.equals("setManager")) {
		String button = request.getParameter("action");
		if ("Menu".equals(button)) {
			response.sendRedirect("userPanel.jsp");
		}else{
			Facade3.getInstance().setManager(request.getParameter("user"));
			response.sendRedirect("setManager.jsp?msg=Permission added successfully");

		}
	} else if (form.equals("removeMovie")) {
		String button = request.getParameter("action");
		if ("Menu".equals(button)) {
			response.sendRedirect("userPanel.jsp");
		}else{
			if (Facade3.getInstance().deleteMovie(request.getParameter("movie"))){
				response.sendRedirect("removeMovie.jsp?Error=Movie Succesfuly removed");
				

			} else {
				response.sendRedirect("removeMovie.jsp?Error=Movie does not exist");
				
				}
		}
	} else if (form.equals("movieListForRating")){
		String button = request.getParameter("action");
		if (button == null){
			response.sendRedirect("movieListForRating.jsp?start=0&type=1&str="+request.getParameter("search"));
		}
		if ("Search".equals(button)) {
			response.sendRedirect("movieListForRating.jsp?start=0&type=1&str="+request.getParameter("search"));
		}if ("Complete List".equals(button)) {
			response.sendRedirect("movieListForRating.jsp?start=0&type=0");
		}

	} else if (form.equals("reportPanel")) {
		String button = request.getParameter("action");
		if ("User's Movies".equals(button)) {
			response.sendRedirect("report1details.jsp");
		} else if ("Best/Worst Movies".equals(button)) {
			response.sendRedirect("report2details.jsp");
		} else if ("Movie Ratings".equals(button)) {
			response.sendRedirect("report3details.jsp");
		} else if ("Movies Pie Chart".equals(button)) {
			response.sendRedirect("report4details.jsp");
		} else if ("Movie Raters".equals(button)) {
			response.sendRedirect("report5details.jsp");
		} else out.println(button);

	} else if (form.equals("r1details")) {
		String us=request.getParameter("user");
		String rate=request.getParameter("rate");
		response.sendRedirect("reportList.jsp?type=user&p1="+us+"&p2="+rate);

	} else if (form.equals("r2details")) {
		String num=request.getParameter("num");
		String dir=request.getParameter("direction");
		if (dir.equals("top"))
			dir="true";
		else dir="false";
		response.sendRedirect("reportList.jsp?type=bla&p1="+num+"&p2="+dir);

	} else if (form.equals("r3details")) {
		String movie=request.getParameter("movie");
		response.sendRedirect("graphReport.jsp?type=bar&p="+movie);

	} else if (form.equals("r4details")) {
		String num=request.getParameter("num");
		String dir=request.getParameter("direction");
		if (dir.equals("top"))
			dir="true";
		else dir="false";
		response.sendRedirect("graphReport.jsp?type=pie&p="+num+"&b="+dir);

	} else if (form.equals("r5details")) {
		String movie=request.getParameter("movie");
		String rate=request.getParameter("rate");
		response.sendRedirect("usersReport.jsp?movie="+movie+"&min="+rate);

	} else {
		out.println("unknown request");
	}
%>
</body>
</html>